/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package yandexfotkibackup;


import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

/**
 *
 * @author petr
 */
public class ReadXMLDownloadHendler extends DefaultHandler {

    private boolean tagsInsideImage = false;
    private String thisElement = "";
    private boolean needToParse = false;
    private PhotoInfo currentPhoto;
    private List<PhotoInfo> collectionPhotos;
    private String linkNes;

    public ReadXMLDownloadHendler() {
        collectionPhotos = new ArrayList<>();
    }

    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
        thisElement = qName;
        if (!tagsInsideImage && "link".equals(qName)) {
            linkNes = nextPageLink(attributes);
            if (getLinkNes() != null) {
                System.out.println(getLinkNes());
            } else {
                System.out.println("No text pages");
            }
        }
        switch (qName) {
            case "entry":
                tagsInsideImage = true;
                currentPhoto = new PhotoInfo();
                break;
            case "f:img":
                if (tagsInsideImage) {
                    imageImageHandler(attributes);
                }
                break;
            case "summary":
            case "title":
            case "id":
                needToParse = true;
                break;

        }
        needToParse &= tagsInsideImage;

    }

    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {
        if (qName.equalsIgnoreCase("entry")) {
            tagsInsideImage = false;
            getCollectionAlboms().add(currentPhoto);
        }
    }

    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {
        if (!needToParse) {
            return;
        }
        needToParse = false;
        if (thisElement.equalsIgnoreCase("id")) {
            String id = new String(ch, start, length);
            currentPhoto.setId(id);
        }
        if (thisElement.equalsIgnoreCase("title")) {
            String title = new String(ch, start, length);
            currentPhoto.setName(title);
        }
        if (thisElement.equalsIgnoreCase("summary")) {
            String summary = new String(ch, start, length);
            currentPhoto.setSummary(summary);
        }
    }

    /**
     * @return the collectionAlboms
     */
    public List<PhotoInfo> getCollectionAlboms() {
        return collectionPhotos;
    }

    /**
     * @param collectionAlboms the collectionAlboms to set
     */
    public void setCollectionAlboms(List<PhotoInfo> collectionAlboms) {
        this.collectionPhotos = collectionAlboms;
    }

    private void imageImageHandler(Attributes attributes) {
        String str;
        try {
            str = attributes.getValue("size");
            if (str != null && str.equals("orig")) {
                String url = attributes.getValue("href");
                currentPhoto.setUrlDownload(url);
                System.out.println("UrlDownload : " + url);

                String byteSizeStr = attributes.getValue("bytesize");
                int bytesize = Integer.parseInt(byteSizeStr);
                currentPhoto.setBytesize(bytesize);
                System.out.println("Size : " + bytesize);


            }
        } catch (NumberFormatException e) {
            Logger.getLogger(ReadXMLAllAlbomsHendler.class.getName()).log(Level.SEVERE, null, e);
        }
    }

    private String nextPageLink(Attributes attributes) {
        String str = attributes.getValue("rel");
        if (str.equals("next")) {
            return attributes.getValue("href");
        }
        return null;
    }

    /**
     * @return the linkNes
     */
    public String getLinkNes() {
        return linkNes;
    }
    public void setLinkNes(String linkNes) {
        this.linkNes = linkNes;
    }
}
